package com.dunqi.gpm.shunqing.home.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.dunqi.gpm.shunqing.R;
import com.dunqi.gpm.shunqing.common.api.ApiFactory;
import com.dunqi.gpm.shunqing.common.api.RxSchedulers;
import com.dunqi.gpm.shunqing.common.base.BaseAct;
import com.dunqi.gpm.shunqing.common.base.ComObserver;
import com.dunqi.gpm.shunqing.common.base.CommonAdapter;
import com.dunqi.gpm.shunqing.common.base.CommonViewHolder;
import com.dunqi.gpm.shunqing.common.base.Data;
import com.dunqi.gpm.shunqing.common.view.ComTitleBar;
import com.dunqi.gpm.shunqing.common.view.NoScrollGridView;
import com.dunqi.gpm.shunqing.home.bean.YearPlan;
import com.dunqi.gpm.shunqing.home.widget.NormalView;

import java.util.Arrays;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;

import static com.dunqi.gpm.shunqing.home.constance.Constance.PROJECTID;
import static com.dunqi.gpm.shunqing.home.constance.Constance.YEAR;

public class YearPlanDetailActivity extends BaseAct {
    @BindView(R.id.title_bar)
    ComTitleBar titleBar;
    @BindView(R.id.title_1_tv)
    TextView title1Tv;
    @BindView(R.id.report_cycle_view)
    NormalView reportCycleView;

    @BindView(R.id.plan_start_time_view)
    NormalView planStartTimeView;
    @BindView(R.id.plan_end_time_view)
    NormalView planEndTimeView;
    @BindView(R.id.build_kind_view)
    NormalView buildKindView;
    @BindView(R.id.last_year_completed_money_view)
    NormalView lastYearCompletedMoneyView;
    @BindView(R.id.last_year_Image_progress_view)
    NormalView lastYearImageProgressView;
    @BindView(R.id.whole_year_content_tv)
    NormalView wholeYearContentTv;
    @BindView(R.id.title_2_tv)
    TextView title2Tv;
    @BindView(R.id.total_money_view)
    NormalView totalMoneyView;
    @BindView(R.id.center_money_view)
    NormalView centerMoneyView;
    @BindView(R.id.center_money_zss)
    NormalView center_money_zss;
    @BindView(R.id.society_money_view)
    NormalView societyMoneyView;
    @BindView(R.id.province_money_view)
    NormalView provinceMoneyView;
    @BindView(R.id.city_money_view)
    NormalView cityMoneyView;
    @BindView(R.id.county_money_view)
    NormalView countyMoneyView;
    @BindView(R.id.bank_money_view)
    NormalView bankMoneyView;
    @BindView(R.id.get_money_view)
    NormalView getMoneyView;
    @BindView(R.id.private_money_view)
    NormalView privateMoneyView;
    @BindView(R.id.financing_view)
    NormalView financingView;
    @BindView(R.id.other_money_view)
    NormalView otherMoneyView;
    @BindView(R.id.determined_money_view)
    NormalView determinedMoneyView;
    @BindView(R.id.des_view)
    NormalView desView;
    @BindView(R.id.title_3_tv)
    TextView title3Tv;
    @BindView(R.id.grid_view)
    NoScrollGridView gridView;
    @BindView(R.id.scrollView)
    ScrollView scrollView;

    @BindView(R.id.center_bank_money_view)
    NormalView centerBankMoneyView;
    @BindView(R.id.factory_bank_money_view)
    NormalView factoryBankMoneyView;
    @BindView(R.id.agricultural_bank_money_view)
    NormalView agriculturalBankMoneyView;
    @BindView(R.id.ccb_money_view)
    NormalView ccbMoneyView;

    private int year;
    private long projectId;

    @Override
    protected void refreshData(Bundle savedInstanceState) {
        ApiFactory.getInstance()
                .queryProjectAnnualPlanInfo(projectId, year)
                .compose(RxSchedulers.<Data<YearPlan>>io_main())
                .subscribe(new ComObserver<YearPlan>() {
                    @Override
                    public void onSuccess(YearPlan yearPlan) {
                        if (yearPlan == null) {
                            return;
                        }
                        refreshUi(yearPlan);
                    }

                    @Override
                    public void onFinal() {
                    }

                    @Override
                    public void onSubscribe(Disposable d) {
                    }
                });
    }

    private void refreshUi(YearPlan yearPlan) {
        //        2018/4/26
        reportCycleView.setVisibility(View.GONE);
        getMoneyView.setVisibility(View.GONE);
        privateMoneyView.setVisibility(View.GONE);
        centerBankMoneyView.setVisibility(View.GONE);
        factoryBankMoneyView.setVisibility(View.GONE);
        agriculturalBankMoneyView.setVisibility(View.GONE);
        ccbMoneyView.setVisibility(View.GONE);
        otherMoneyView.setVisibility(View.GONE);
        determinedMoneyView.setVisibility(View.GONE);
        provinceMoneyView.setVisibility(View.GONE);
        cityMoneyView.setVisibility(View.GONE);
        centerMoneyView.setVisibility(View.GONE);
        financingView.setVisibility(View.GONE);


        titleBar.setTitle(yearPlan.getPlanYear() + "年年度计划");
        title1Tv.setText(yearPlan.getPlanYear() + "年年度计划");
        title2Tv.setText(yearPlan.getPlanYear() + "年年度投资计划(万元)");
        title3Tv.setText(yearPlan.getPlanYear() + "年每月形象进度");

        String reportFrequency = yearPlan.getReportFrequency();
        String text;
        if ("report_per_month".equals(reportFrequency)) {
            text = "按月汇报\n每月21日自动生成待汇报，汇报时限是7天";
        } else if ("report_per_10days".equals(reportFrequency)) {
            text = "按旬汇报\n每月9、19、29日自动生成待汇报，汇报时限是1天";
        } else {
            text = "无需汇报";
        }
        reportCycleView.setText(text);
        planStartTimeView.setText(yearPlan.getPlanStartTime());
        //"nationalFund":500.000    "conuntyFund":500.000,"
        center_money_zss.setText(yearPlan.getNationalFund() + "");//中省市资金
        societyMoneyView.setText(yearPlan.getAttractInvestmentFund()+ "");//社会投资
        planEndTimeView.setText(yearPlan.getPlanCompleteTime());
        buildKindView.setText(yearPlan.getConstructionTypeText());
        lastYearCompletedMoneyView.setText(String.valueOf(yearPlan.getCompletedFundLastYear()));
        lastYearImageProgressView.setText(yearPlan.getProgressDescThisYear());
        wholeYearContentTv.setText(yearPlan.getConstructionDescWholeYear());

        totalMoneyView.setText(String.valueOf(yearPlan.getTotalInvestmentAmount()));
        centerMoneyView.setText(String.valueOf(yearPlan.getConuntyFund()));
        privateMoneyView.setText(String.valueOf(yearPlan.getProvinceFund()));
        cityMoneyView.setText(String.valueOf(yearPlan.getMunicipalFund()));
        countyMoneyView.setText(String.valueOf(yearPlan.getConuntyFund()));
        bankMoneyView.setText(String.valueOf(yearPlan.getBankLoadFund()));
        getMoneyView.setText(String.valueOf(yearPlan.getAttractInvestmentFund()));
        privateMoneyView.setText(String.valueOf(yearPlan.getSelfFund()));
        financingView.setText(String.valueOf(yearPlan.getRaisingFund()));
        otherMoneyView.setText(String.valueOf(yearPlan.getOtherFund()));
        determinedMoneyView.setText(String.valueOf(yearPlan.getUndeterminedFund()));
        desView.setText(String.valueOf(yearPlan.getFundSourceDesc()));

        centerBankMoneyView.setText(String.valueOf(yearPlan.getBocLoadFund()));
        factoryBankMoneyView.setText(String.valueOf(yearPlan.getIcbcLoadFund()));
        agriculturalBankMoneyView.setText(String.valueOf(yearPlan.getAbcLoadFund()));
        ccbMoneyView.setText(String.valueOf(yearPlan.getCcbLoadFund()));

        String[] visualProgresses = yearPlan.getVisualProgresses();
        LinearLayout ll_ydxx = findViewById(R.id.ll_ydxx);
        ll_ydxx.removeAllViews();
        for (int i = 0; i < visualProgresses.length; i++) {
            NormalView normalView = new NormalView(getActivity());
            normalView.setdesLength(8);
            normalView.setDes((i + 1) + "月形象进度:");
            normalView.setText(visualProgresses[i]);
            ll_ydxx.addView(normalView, i);
        }
//        if (visualProgresses != null) {
//            gridView.setAdapter(new CommonAdapter<String>(getActivity(), Arrays.asList(visualProgresses)) {
//                @Override
//                public int getLayout(int position) {
//                    return R.layout.item_visual;
//                }
//
//                @Override
//                public void bindData(CommonViewHolder cvh, int position) {
//                    NormalView normalView = cvh.getView(R.id.normal_view);
//                    normalView.setDes((position + 1) + "月形象进度:");
//                    normalView.setText(getItem(position));
//                }
//            });
//        }
//        scrollView.smoothScrollTo(0, 0);
    }

    @Override
    protected void initUiAndListener() {
        Intent intent = getIntent();
        year = intent.getIntExtra(YEAR, 0);
        projectId = intent.getLongExtra(PROJECTID, 0);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_year_plan_detail;
    }

    public static void start(Context context, int year, long projectId) {
        Intent intent = new Intent(context, YearPlanDetailActivity.class);
        intent.putExtra(YEAR, year);
        intent.putExtra(PROJECTID, projectId);
        context.startActivity(intent);
    }

}
